var username;
window.onload = function () {

    //图片切换
    var panduan = 1;

    function change() {
        if (panduan != 3) {
            $("#pic_change" + panduan).animate({opacity: "0"});
            panduan++;
            setTimeout(function () {
                $("#pic_change" + panduan).animate({opacity: "1"});
            }, 1000)
        } else {
            $("#pic_change" + panduan).animate({opacity: "0"});
            panduan = 1;
            setTimeout(function () {
                $("#pic_change" + panduan).animate({opacity: "1"});
            }, 1000)
        }
    }

    setInterval(change, 7000);

    $.get("/login_check", {
    }, function(res) {
        var zhizheng = this;
        if (res == 2) {
            alert('您没有登录或者登录超时，即将为您跳转登录页面');
            setTimeout(function () {
                window.location.href = "/html/1.html";
            },2000)
        }
        else {
            username = res;
        }
    });

    var Main = {
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPass !== '') {
                        this.$refs.form.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateoldPass = (rule, value, callback) => {
                if (value == '') {
                    callback(new Error('请输入登录密码'));
                } else {
                    callback();
                }
            }
            return{
                form: {
                    username: '',
                    oldpass: '',
                    pass: '',
                    checkPass: '',
                    name: '',
                    phone: '',
                },
                rules2: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    oldpass: [
                        { validator: validateoldPass, trigger: 'blur' }
                    ]
                },
            }
        },
        methods: {
            open() {
                this.$alert('如有问题请联系网站技术人员，QQ号：*********', '帮助', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'success',
                            message: `感谢您的支持`
                        });
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            onSubmit(formName) {
                var zhizheng = this;
                this.form.username = username;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        $.ajax({
                            type: "POST",
                            url: "/user_change",
                            data: this.form,
                            dataType: "json",
                            success: function (message) {
                                if (message == 1) {
                                    zhizheng.$alert('修改成功，点击确定后为您跳转登录界面','成功', {
                                        confirmButtonText: '确定',
                                        callback: action => {
                                            window.location.href="/html/1.html";
                                        }
                                    });
                                }
                                else {
                                    zhizheng.$alert('修改失败，您输入的密码错误','失败', {
                                        confirmButtonText: '确定',
                                        callback: action => {
                                            zhizheng.resetForm(formName)
                                        }
                                    });
                                }
                            },
                            error: function (message) {
                                $("#request-process-patent").html("提交数据失败！");
                                alert("真的失败了！我们会尽快与您取得联系");
                            }
                        });
                    }
                    else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }

    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')

}